$NG.AllReady(function(page,{useValuesChange,useUpdateRows}){
	const mstform = $NG.getCmpApi("p_form_dev_cost_budget")
	const dgrid1 = $NG.getCmpApi("p_form_dev_labor_budget")
	const dgrid2 = $NG.getCmpApi("p_form_dev_daliy_budget")
    console.log('正常运行');
    cntMFilter()
    getPtInfoByPhid()
        getCntCount("p_form_dev_cost_budget","u_cnt",getCntInfo)
    useValuesChange(({args})=>{
        console.log(`项目监听`);
        getPtInfoByPhid()
        cntMFilter()
        getCntCount("p_form_dev_cost_budget","u_cnt",getCntInfo)
    }, "p_form_dev_cost_budget.phid_pc")

    useValuesChange(({args})=>{
        getCntInfo()
    }, "p_form_dev_cost_budget.u_cnt")
    // 监听表格研发人工
    useUpdateRows(({args})=>{
        sum()
        console.log(`监听编辑`);
    },"p_form_dev_daliy_budget")
    useUpdateRows(({args})=>{
        console.log(`监听编辑`);
        sum()
    },"p_form_dev_labor_budget")
    function sum(){
        const _row = dgrid1.getRows()
        const _row2 = dgrid2.getRows()
        let _count = 0
        let _count2 = 0;
        console.log(_row,_row2);
        _row.forEach(element => {
            console.log(element.u_labor_amt);
            _count+=parseFloat(element.u_labor_amt)?parseFloat(element.u_labor_amt):0
        });
        _row2.forEach(element => {
            console.log(element.u_dev_daliy_amt);
            _count2+=parseFloat(element.u_dev_daliy_amt)?parseFloat(element.u_dev_daliy_amt):0
        });
        console.log(_count,_count2);
        $NG.updateState((updater)=>{
            updater.data.p_form_dev_cost_budget.setProps({
                u_total_labor_amt:_count,
                u_total_daily_amt:_count2,
                u_total_dev_amt:_count + _count2
            })
        })
    }
	if (page.oType == "add" || page.oType == "edit") {
		$NG.updateState((updater) => {
			updater.data.p_form_dev_cost_budget.setProps({
				u_fitime:
					"FY" + formatDate(new Date(), "yyyy-mm").replace("-", ".M"),
			});
		});
		
	}
	//表头多字段设置属性
	$NG.updateUI((updater) => {
		updater.fieldSetForm.p_form_dev_cost_budget.u_fitime.setProps({
			hidden: true, //隐藏
		});
	});
	setTimeout(()=>{
		console.log(`校验隐藏的标识日期`, mstform.getItem("u_fitime").getValue());
	},1000)
    // 获取项目信息
    function getPtInfoByPhid() {
		if (mstform.getItem("phid_pc").getValue()) {
			$NG.execServer(
				"getPtInfoByPhid",
				{ phid: mstform.getItem("phid_pc").getValue() },
				function (res) {
					const datas = JSON.parse(res.data)[0].extendObjects;
					console.log(`获取项目信息`,datas);
					//帮助字段赋值
					$NG.updateState((updater) => {
						updater.data.p_form_dev_cost_budget.setProps({
							u_pc_no: datas.pc_no, //项目编码
							//项目类型
							u_pc_type:{
								label:datas.type_name,
								value:datas.type_phid
							},
							u_sqgw: {
								//售前顾问
								value: datas.user_sqgw_phid,
								label: datas.user_sqgw_name,
							},
							u_division: {
								//事业部or组织
								value: datas.phid_org_phid,
								label: datas.phid_org_name,
							},
							u_regional_center: {
								//区域中心
								value: datas.regional_center_phid,
								label: datas.regional_center_name,
							},
                            u_dept: {
                                //部门
                                value: datas.dept_phid,
                                label: datas.dept_name,
                            },
                            u_region:{
                                //部门
                                value: datas.dept_phid,
                                label: datas.dept_name,
                            },
							u_product_line: {
								//产品线
								value: datas.midwife_phid,
								label: datas.midwife_name,
							},
						});
					});
				}
			);
		}
	}

    // 获取合同信息
	function getCntInfo(){
        $NG.execServer(
            "getCntMInfoByPhid",
            { phid: mstform.getItem("u_cnt").getValue() },
            function (res) {
                const datas = JSON.parse(res.data)[0].extendObjects;
                console.log(`合同：`,datas);
                //帮助字段赋值
                $NG.updateState((updater) => {
                    updater.data.p_form_dev_cost_budget.setProps({
                        u_agreed_sts_dt: datas.start_dt, //计划完工日期
                        u_agreed_pcl_dt: datas.end_dt, //计划竣工日期
                        u_cnt_no:datas.pcm_no,
                        u_invoice_type:{
                            label:datas.invoice_type_name,
                            value:datas.invoice_type_phid
                        }
                    });
                });
            }
        );
    }

    // 过滤合同
    function cntMFilter() {
		if (!mstform.getItem("phid_pc").getValue()) {
			return;
		}
        console.log(`开始过滤`);
		$NG.updateUI((updater, state) => {
			updater.fieldSetForm.p_form_dev_cost_budget.u_cnt.setProps({
				onBeforeOpen: async () => {
					var phid_pc = $NG
						.getCmpApi("p_form_dev_cost_budget")
						.getItem("phid_pc")
						.getValue();
					if (!phid_pc) {
						$NG.alert("请先选择项目");
						return false;
					} else {
						return true;
					}
				},
				...(!!$NG
					.getCmpApi("p_form_dev_cost_budget")
					.getItem("phid_pc")
					.getValue() && {
					clientSqlFilter: {
						bill_type: 1,
						phid_pc: $NG
							.getCmpApi("p_form_dev_cost_budget")
							.getItem("phid_pc")
							.getValue(),
					},
				}),
			});
		});
	}
	
	    // 格式化日期
function formatDate(dateStr, outputFormat) {
    const date = new Date(dateStr);

    if (isNaN(date)) {
        return "无效的日期字符串";
    }

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');

    let formattedDate;

    switch (outputFormat) {
        case "yyyy-mm-dd":
            formattedDate = `${year}-${month}-${day}`;
            break;
        case "yyyy-mm-dd HH:mm":
            formattedDate = `${year}-${month}-${day} ${hours}:${minutes}`;
            break;
        case "yyyy-mm":
            formattedDate = `${year}-${month}`;
        break;
        // 可以根据需要继续添加其他格式的处理逻辑
        default:
            formattedDate = "不支持的输出格式";
    }

    return formattedDate;
}
})

//获取合同数量
function getCntCount(table,key,fun){
    if(!$NG.getCmpApi(table).getItem("phid_pc").getValue()) return 
    $NG.execServer("getCntCount",{type:1,phid_pc:$NG.getCmpApi(table).getItem("phid_pc").getValue()},(res)=>{
      console.log(res,'count');
      switch(res.count){
        case 0:
          return
        case 1:
          const datas = JSON.parse(res.data)[0].extendObjects
          const temp_map = {}
          temp_map[key] = datas.phid
          temp_map[`${key}_EXName`] = datas.bill_name
          $NG.updateState((updater)=>{
            updater.data[table].setProps({
              ...temp_map
            })
            setTimeout(()=>{
              if(fun){
                fun()
              }
            },200)
          })
      }
    })
  }